package parts.spring;

import java.awt.geom.Point2D;
import parts.mass.Mass;
import applet.Simulation;
import applet.forces.Force;


/**
 * @author Jesse Starr and Guy Tracy
 *
 * The muscle is a subclass of spring.
 * A muscle behaves like a spring, but
 * the rest length is changed according
 * to the equation for harmonic oscillation,
 * which makes the spring move constantly.
 */
public class Muscle extends Spring {

    private double myAmp;

    /**
     * Creates a muscle (oscillating spring).
     *
     * @param start first mass it is connected to
     * @param end second mass it is connected to
     * @param length of the spring
     * @param kVal of the spring
     * @param amp of the oscillation
     */
    public Muscle (Mass start, Mass end, double length,
            double kVal, double amp) {
        super(start, end, length, kVal);
        myAmp = amp;
    }

    @Override
    public void update (Simulation canvas, double dt) {
        double newlen;
        double freq;
        double reducedmass = (getMyStart().getMass() * getMyEnd().getMass())
                / (getMyStart().getMass() + getMyEnd().getMass());

        freq = (1 / (2 * Math.PI)) * Math.sqrt(getMyK() / reducedmass);
        newlen = myAmp * Math.cos(2 * Math.PI * freq * dt);

        setMyLength(newlen);     
        super.update(canvas, dt);
    }
}
